package DBHandle;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.xml.ws.RespectBinding;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.mail.iap.Response;

public class Login extends ActionSupport
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 5278348997728491972L;
	private String username=null;
	private String password=null;
	private String tip=null;
	
	public String getUsername()
	{
		return username;
	}
	public void setUsername(String username)
	{
		this.username=username;
	}
	public String getPassword()
	{
		return password;
	}
	public void setPassword(String password)
	{
		this.password=password;
	}
	public String execute() throws Exception
	{
		DBConn.DBConnManager connManager=new DBConn.DBConnManager();
		ResultSet rs=null;
		Statement stmt=null;
		
		//连接Mysql数据库
		Connection conn=connManager.getConnection("Mysql");
		if(conn==null)
		{
			return ("failure");
		}
			stmt=conn.createStatement();
			//查询是否存在此用户
			String sql="Select * from user_table where UserName = '"+username
						+"' and Password = '"+password+"'";
			rs=stmt.executeQuery(sql);
			rs.last();
			//存在此用户则跳转到主界面
			if(rs.getRow()==1)
			{
				rs.close();
				stmt.close();
				connManager.releaseConnection("Mysql", conn);
				ActionContext.getContext().getSession().put("username", username);
				//session.put("username", username);
				//session.put("password", password);
				return ("success");
			}
			//跳转到登录失败界面
			else
			{
				rs.close();
				stmt.close();
				connManager.releaseConnection("Mysql", conn);
				return ("failure");
			}
	}
}